this._grid.removeItem(item);
}
+ _getItemPosition(item) {
+ const { itemsPerPage } = this._grid;
+
+ let iconIndex = this._orderedItems.indexOf(item);
+ if (iconIndex === -1)
+ iconIndex = this._orderedItems.length - 1;
+
+ const page = Math.floor(iconIndex / itemsPerPage);
+ const position = iconIndex % itemsPerPage;
+
+ return [page, position];
+ }
+
_redisplay() {
let oldApps = this._orderedItems.slice();
let oldAppIds = oldApps.map(icon => icon.id);
- let newApps = this._loadApps().sort(this._compareItems);
+ let newApps = this._loadApps().sort(this._compareItems.bind(this));
let newAppIds = newApps.map(icon => icon.id);
let addedApps = newApps.filter(icon => !oldAppIds.includes(icon.id));
});
// Add new app icons
- const { itemsPerPage } = this._grid;
addedApps.forEach(icon => {
- let iconIndex = newApps.indexOf(icon);
-
- this._orderedItems.splice(iconIndex, 0, icon);
- this._items.set(icon.id, icon);
-
- const page = Math.floor(iconIndex / itemsPerPage);
- const position = iconIndex % itemsPerPage;
- this._grid.addItem(icon, page, position);
+ const [page, position] = this._getItemPosition(icon, newApps);
+ this._addItem(icon, page, position);
});
this._viewIsReady = true;
// supposed to be and reinsert it where it's sorted.
let oldIdx = this._orderedItems.indexOf(item);
this._orderedItems.splice(oldIdx, 1);
- let newIdx = Util.insertSorted(this._orderedItems, item, this._compareItems);
+ let newIdx = Util.insertSorted(this._orderedItems, item, this._compareItems.bind(this));
this._grid.removeItem(item);
return this._appInfoList;
}
+ _getItemPosition(item) {
+ return this._pageManager.getAppPosition(item.id);
+ }
+
+ _compareItems(a, b) {
+ const [aPage, aPosition] = this._pageManager.getAppPosition(a.id);
+ const [bPage, bPosition] = this._pageManager.getAppPosition(b.id);
+
+ if (aPage === -1 && bPage === -1)
+ return a.name.localeCompare(b.name);
+ else if (aPage === -1)
+ return 1;
+ else if (bPage === -1)
+ return -1;
+
+ if (aPage !== bPage)
+ return aPage - bPage;
+
+ return aPosition - bPosition;
+ }
+
_loadApps() {
let appIcons = [];
this._appInfoList = Shell.AppSystem.get_default().get_installed().filter(appInfo => {